﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Farmings
{
    /// <summary>
    ///添加养殖信息命令处理
    /// </summary>
    public class AddFarmingCommandHandlter : IRequestHandler<AddFarmingCommand, ApiResult<int>>
    {
        /// <summary>
        /// 养殖仓储
        /// </summary>
        private readonly IBaseRepository<FarmingModel> farmingRep;

       /// <summary>
       /// 映射
       /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingRep">养殖信息</param>
        /// <param name="mapper">映射</param>
        public AddFarmingCommandHandlter(IBaseRepository<FarmingModel> farmingRep,IMapper mapper)
        {
            this.farmingRep = farmingRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(AddFarmingCommand request, CancellationToken cancellationToken)
        {

            //添加养殖信息

            var  farming=mapper.Map<FarmingModel>(request);

            await farmingRep.AddAsync(farming);

            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加养殖信息成功"
            };
        }
    }
}
